package com.voa.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.constants.SQLConstant;
import org.team4.constants.StringConstant;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.DateTimeHelper;
import org.team4.util.LoginBean;
import org.team4.util.Meta;
import org.team4.util.SearchUtil;
import org.team4.util.Validate;

import com.voa.constants.OASQLConstant;
import com.voa.constants.OAStringConstant;
import com.voa.dao.BlogDAO;
import com.voa.dao.BlogReplyDAO;
import com.voa.domain.Blog;
import com.voa.domain.BlogTags;
import com.voa.vo.BlogAgg;

/**
 * <p>
 * 程序名称： BlogService.java
 * </p>
 * <p>
 * 程序说明： 日志服务
 * </p>
 * <p>
 * 版权信息： Copyright 深圳市维远泰克科技有限公司
 * </p>
 * <p>
 * 时间： Feb 11, 2011 11:39:08 AM
 * </p>
 * 
 * @author： Marker.King
 * @version： Ver 0.1
 */
@Service
public class BlogService implements IService {

	private BlogDAO blogDAO;
	private BlogReplyDAO blogReplyDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		blogDAO = (BlogDAO) DAOFactory.getDAO(sessionFactory, BlogDAO.class);
		blogReplyDAO = (BlogReplyDAO) DAOFactory.getDAO(sessionFactory,
				BlogReplyDAO.class);
	}

	public List<Blog> findBlog(Date forDate) throws CRUDException {
		if (forDate != null) {
			return this.convertTags(blogDAO.findOrder(
					OASQLConstant.BLOG_BY_DATE
							+ SQLConstant.getUsersWhere(Meta
									.getUsers(OAStringConstant.BLOG_ID)),
					"forDate desc,creator asc,isShare desc", forDate));
		} else {
			Date[] day7 = DateTimeHelper.get7Day(new Date());
			return this.convertTags(blogDAO.findOrder(
					OASQLConstant.BLOG_7_DAY
							+ SQLConstant.getUsersWhere(Meta
									.getUsers(OAStringConstant.BLOG_ID)),
					"forDate desc,creator asc,isShare desc", day7[0], day7[1]));
		}
	}

	public List<BlogAgg> findBlogAgg(Date forDate) throws CRUDException {
		if (forDate != null) {
			StringBuffer where = new StringBuffer();
			where.append("and forDate = '");
			where.append(DateTimeHelper.formatDateTime("yyyy-MM-dd", forDate));
			where.append("' ");
			where.append(SQLConstant.getSQLUsersWhere(Meta
					.getUsers(OAStringConstant.BLOG_ID)));
			return blogDAO.findBlogAggregation(where.toString());
		} else {
			Date[] day7 = DateTimeHelper.get7Day(new Date());
			StringBuffer where = new StringBuffer();
			where.append(" and (forDate between '");
			where.append(DateTimeHelper.formatDateTime("yyyy-MM-dd HH:mm:ss",
					day7[0]));
			where.append("' and '");
			where.append(DateTimeHelper.formatDateTime("yyyy-MM-dd HH:mm:ss",
					day7[1]));
			where.append("')");
			where.append(SQLConstant.getSQLUsersWhere(Meta
					.getUsers(OAStringConstant.BLOG_ID)));
			return blogDAO.findBlogAggregation(where.toString());
		}
	}

	public List<Blog> searchBlog(String searchType, List<String> searchValue)
			throws CRUDException {
		String ss = SearchUtil.getString(new String[] { "contents", "tags",
				"forDate", "creator.id" },// 高级查询条件
				new String[] { SearchUtil.STRING_LIKE, SearchUtil.STRING_LIKE,
						SearchUtil.BETWEEN_DATE, SearchUtil.IN },// 查询类型
				searchType,// 与或类型
				searchValue);// 查询值
		return this.convertTags(blogDAO.findOrder(
				OASQLConstant.BLOG_SCOPE_LIST
						+ ss
						+ SQLConstant.getUsersWhere(Meta
								.getUsers(OAStringConstant.BLOG_ID)),
				"forDate desc,creator asc,isShare desc"));
	}

	public List<BlogAgg> searchBlogAgg(String searchType,
			List<String> searchValue) throws CRUDException {
		String ss = SearchUtil.getString(new String[] { "contents", "tags",
				"forDate", "creator" },// 高级查询条件
				new String[] { SearchUtil.STRING_LIKE, SearchUtil.STRING_LIKE,
						SearchUtil.BETWEEN_DATE, SearchUtil.IN },// 查询类型
				searchType,// 与或类型
				searchValue);// 查询值
		return blogDAO.findBlogAggregation(ss
				+ SQLConstant.getSQLUsersWhere(Meta
						.getUsers(OAStringConstant.BLOG_ID)));
	}

	public Blog findBlogById(String id) throws CRUDException {
		return blogDAO.get(id);
	}

	public void saveBlog(Blog blog) throws CRUDException {
		blog.setCreator(LoginBean.getLoginBean().getUser());
		blog.setCreateTime(new Date());
		if (Validate.notNull(blog.getTags())) {
			blog.setTags(blog.getTags().substring(0,
					blog.getTags().length() - 1));
		}
		if (!StringConstant.TRUE.equals(blog.getIsShare())) {
			blog.setIsShare(StringConstant.FALSE);
		}
		blogDAO.save(blog);
	}

	public void updateBlog(Blog blog) throws CRUDException {
		Blog old = blogDAO.get(blog.getId());
		old.setContents(blog.getContents());
		old.setEditTime(new Date());
		old.setForDate(blog.getForDate());
		old.setIsShare(blog.getIsShare());
		old.setTags(blog.getTags());
		if (Validate.notNull(old.getTags())) {
			old.setTags(blog.getTags()
					.substring(0, blog.getTags().length() - 1));
		}
		if (!StringConstant.TRUE.equals(old.getIsShare())) {
			old.setIsShare(StringConstant.FALSE);
		}
		blogDAO.update(old);
	}

	public void deleteBlog(String id) throws CRUDException {
		Blog blog = blogDAO.get(id);
		blogReplyDAO.deleteAll(blogReplyDAO.findByProperty("blog", blog));
		blogDAO.delete(blog);
	}

	private List<Blog> convertTags(List<Blog> blogList) {
		List<Blog> resultList = new ArrayList<Blog>();
		for (Iterator iterator = blogList.iterator(); iterator.hasNext();) {
			Blog blog = (Blog) iterator.next();
			if (Validate.notNull(blog.getTags())) {
				String[] tags = blog.getTags().split(",");
				List<BlogTags> btList = new ArrayList<BlogTags>();
				for (int i = 0; i < tags.length; i++) {
					String[] tagsValue = tags[i].split("-");
					BlogTags bt = new BlogTags();
					bt.setName(tagsValue[0]);
					bt.setColor(tagsValue[1]);
					btList.add(bt);
				}
				blog.setTagsList(btList);
			}
			resultList.add(blog);
		}
		return resultList;
	}

}
